#include "mod_geom_qt.h"
#include <qcolordialog.h>

mod_geom_qt::mod_geom_qt(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);

	QGLFormat fmt;
    fmt.setVersion(3,3);
    //fmt.setProfile(QGLFormat::CoreProfile);//DOESN'T WORK WITH CORE PROFILE(NOTHING ON SCREEN)
    fmt.setProfile(QGLFormat::CompatibilityProfile);
	fmt.setDoubleBuffer(true);
	fmt.setDepth(false);
	fmt.setAlpha(true);
	
	fmt.setDepthBufferSize(0);
	fmt.setDoubleBuffer(true);
	glWidget = new CAD_CAM::GLWidget(fmt);

    connect(ui.xAxisSlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setRPlane(int)));
	connect(ui.xAxisSlider, SIGNAL(valueChanged(int)), this, SLOT(setRPlaneValue(int)));
	
	// sizing events
	connect(ui.torusHeight, SIGNAL(valueChanged(double)), glWidget, SLOT(setHeight(double)));
	connect(ui.torusWidth, SIGNAL(valueChanged(double)), glWidget, SLOT(setWidth(double)));
	connect(ui.torusLength, SIGNAL(valueChanged(double)), glWidget, SLOT(setLength(double)));

	// 
	connect(ui.verticesNumberBox, SIGNAL(valueChanged(int)), glWidget, SLOT(setTorusVerticesNumber(int)));
	connect(ui.stripesNumberBox, SIGNAL(valueChanged(int)), glWidget, SLOT(setTorusStripesNumber(int)));

	// keyboard events handling
	connect(ui.grabMouseCheck, SIGNAL(clicked(bool)), this, SLOT(grabKeyboard(bool)));

	// stereo mode
	connect(ui.stereoCheckBox, SIGNAL(clicked(bool)), glWidget, SLOT(setStereo(bool)));
	// colors
	connect(ui.leftEyeButton, SIGNAL(clicked(bool)), this, SLOT(changeLeftColor(bool)));
	connect(ui.rightEyeButton, SIGNAL(clicked(bool)), this, SLOT(changeRightColor(bool)));

	// eyes width param
	connect(ui.eyesWidthSpin, SIGNAL(valueChanged(double)), glWidget, SLOT(setEyesWidth(double)));
	
	ui.mainLayout->addWidget(glWidget);
	grabKeyboard(true);

}

mod_geom_qt::~mod_geom_qt()
{
	delete glWidget;
}

void mod_geom_qt::grabKeyboard(bool grab)
{
	if(grab)
		glWidget->grabKeyboard();
	else
		glWidget->releaseKeyboard();
}

void mod_geom_qt::setRPlaneValue(int r)
{
	ui.rPlaneValueLabel->setText(QString::number(glWidget->getRplane(), 'g', 3));
}

void mod_geom_qt::changeLeftColor(bool value)
{
	QColor c = glWidget->getLeftColor();
	c = QColorDialog::getColor(c, this);
	glWidget->setLeftColor(c);
}

void mod_geom_qt::changeRightColor(bool value)
{
	QColor c = glWidget->getRightColor();
	c = QColorDialog::getColor(c, this);
	glWidget->setRightColor(c);
}

